#include "pagepacketdetail.h"

extern int channelMapping[6];

subPaintPage::subPaintPage(QWidget *parent) :
    QWidget(parent)
{
    setStyleSheet(".subPaintPage{border:1px solid gray;}"
                  ".QLabel{color:gray;font-size:14px;}"
                  );

    labIndex = new QLabel("0",this);
    labIndex->setGeometry(5,5,30,20);

    QLabel *labMaxN = new QLabel("Max:");
    labMaxN->setFixedSize(30,20);

    maxValue = new QLabel("000000");
    maxValue->setFixedSize(200,20);

    labChnl = new QLabel("Ua");
    labChnl->setStyleSheet(".QLabel{color:yellow; font-size:20px;font-weight:bold;}");
    labChnl->hide();

    QHBoxLayout *layoutChnl = new QHBoxLayout;
    layoutChnl->addStretch(1);
    layoutChnl->addWidget(labChnl);

    QHBoxLayout *layoutMax = new QHBoxLayout;
    layoutMax->addWidget(labMaxN);
    layoutMax->addWidget(maxValue);
    layoutMax->addStretch(1);

    QVBoxLayout *layoutMain = new QVBoxLayout;
    layoutMain->setContentsMargins(3,5,5,3);
    layoutMain->addLayout(layoutChnl);
    layoutMain->addStretch(1);
    layoutMain->addLayout(layoutMax);

    setLayout(layoutMain);
}

subPaintPage::~subPaintPage()
{

}

void subPaintPage::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);

    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void subPaintPage::setIndex(int index)
{
    labIndex->setText(QString::number(index+1));
}

void subPaintPage::setChannel(int index)
{
    qDebug()<<"setChannel(int index)"<<index;

    labChnl->show();

    switch(index)
    {
    case -1:
        labChnl->hide();
        break;
    case 0:
        labChnl->setText("Ua");
        break;
    case 1:
        labChnl->setText("Ub");
        break;
    case 2:
        labChnl->setText("Uc");
        break;
    case 3:
        labChnl->setText("Ia");
        break;
    case 4:
        labChnl->setText("Ib");
        break;
    case 5:
        labChnl->setText("Ic");
        break;
    default:
        labChnl->hide();
    }
}

pagePacketDetail::pagePacketDetail(QWidget *parent) :
    QWidget(parent)
{
    for(int i=0; i<12; i++)
    {
        subPaintPage *subPage = new subPaintPage(this);

        subPage->setIndex(i);

        subPages.append(subPage);
    }

    QGridLayout *layoutSubPages = new QGridLayout;
    layoutSubPages->setVerticalSpacing(2);
    layoutSubPages->setHorizontalSpacing(2);
    layoutSubPages->addWidget(subPages.at(0), 0 ,0 , 1, 1);
    layoutSubPages->addWidget(subPages[1], 0 ,1 , 1, 1);
    layoutSubPages->addWidget(subPages[2], 0 ,2 , 1, 1);
    layoutSubPages->addWidget(subPages[3], 0 ,3 , 1, 1);
    layoutSubPages->addWidget(subPages[4], 1 ,0 , 1, 1);
    layoutSubPages->addWidget(subPages[5], 1 ,1 , 1, 1);
    layoutSubPages->addWidget(subPages[6], 1 ,2 , 1, 1);
    layoutSubPages->addWidget(subPages[7], 1 ,3 , 1, 1);
    layoutSubPages->addWidget(subPages[8], 2 ,0 , 1, 1);
    layoutSubPages->addWidget(subPages[9], 2 ,1 , 1, 1);
    layoutSubPages->addWidget(subPages[10], 2 ,2, 1, 1);
    layoutSubPages->addWidget(subPages[11], 2 ,3 , 1, 1);

    btnPre = new QPushButton();
    btnPre->setText("上\n一\n页");
    btnPre->setFixedSize(60,120);
    btnPre->setStyleSheet("color:gray;");
    btnNxt = new QPushButton();
    btnNxt->setText("下\n一\n页");
    btnNxt->setFixedSize(60,120);
    btnNxt->setStyleSheet("color:gray;");

    connect(btnPre, SIGNAL(clicked(bool)), this, SLOT(changePage()));
    connect(btnNxt, SIGNAL(clicked(bool)), this, SLOT(changePage()));

    QHBoxLayout *layoutMain = new QHBoxLayout;
    layoutMain->setSpacing(0);
    layoutMain->addWidget(btnPre);
    layoutMain->addSpacing(10);
    layoutMain->addLayout(layoutSubPages);
    layoutMain->addSpacing(10);
    layoutMain->addWidget(btnNxt);

    setLayout(layoutMain);

    widgetSetChnl = new QWidget(this);
    widgetSetChnl->setFixedSize(210,100);
    widgetSetChnl->setStyleSheet(".QWidget{background-color:rgba(100,100,100,150);}");

    QLabel *labSetChnl = new QLabel("标记为：");

    QPushButton *btnBack = new QPushButton();
    btnBack->setStyleSheet("border-image:url(:/image/back.png);");
    btnBack->setFixedSize(25,25);
    connect(btnBack, SIGNAL(clicked(bool)), widgetSetChnl, SLOT(hide()));

    labSetChnl->setStyleSheet("background-color:rgba(0,0,0,0);color:white;border-radius:3px;");

    QSignalMapper* mapper = new QSignalMapper(this);
    connect(mapper, SIGNAL(mapped(const QString&)), this, SLOT(setChnl(const QString&)));

    QPushButton *btnUa = new QPushButton("Ua");
    mapper->setMapping( btnUa, "0");
    connect(btnUa, SIGNAL(clicked(bool)), mapper, SLOT(map()));

    QPushButton *btnUb = new QPushButton("Ub");
    mapper->setMapping( btnUb, "1");
    connect(btnUb, SIGNAL(clicked(bool)), mapper, SLOT(map()));

    QPushButton *btnUc = new QPushButton("Uc");
    mapper->setMapping( btnUc, "2");
    connect(btnUc, SIGNAL(clicked(bool)), mapper, SLOT(map()));
    btnUc->setFixedWidth(63);
    QPushButton *btnIa = new QPushButton("Ia");
    mapper->setMapping( btnIa, "3");
    connect(btnIa, SIGNAL(clicked(bool)), mapper, SLOT(map()));

    QPushButton *btnIb = new QPushButton("Ib");
    mapper->setMapping( btnIb, "4");
    connect(btnIb, SIGNAL(clicked(bool)), mapper, SLOT(map()));

    QPushButton *btnIc = new QPushButton("Ic");
    mapper->setMapping( btnIc, "5");
    connect(btnIc, SIGNAL(clicked(bool)), mapper, SLOT(map()));

    QGridLayout *layoutSetChnl = new QGridLayout;
    layoutSetChnl->addWidget(labSetChnl, 0, 0, 1, 1);
    layoutSetChnl->addWidget(btnBack, 0 ,2, 1, 1, Qt::AlignRight);
    layoutSetChnl->addWidget(btnUa, 1, 0,1, 1 );
    layoutSetChnl->addWidget(btnUb, 1, 1,1, 1 );
    layoutSetChnl->addWidget(btnUc, 1, 2,1, 1 );
    layoutSetChnl->addWidget(btnIa, 2, 0,1, 1 );
    layoutSetChnl->addWidget(btnIb, 2, 1,1, 1 );
    layoutSetChnl->addWidget(btnIc, 2, 2,1, 1 );

    widgetSetChnl->setLayout(layoutSetChnl);

    widgetSetChnl->hide();

    pageID = 0;
}

pagePacketDetail::~pagePacketDetail()
{

}

void pagePacketDetail::mousePressEvent(QMouseEvent *e)
{
    int clicked_index = -1;

    if(widgetSetChnl->underMouse())
    {
        return QWidget::mousePressEvent(e);
    }

    for(int i=0; i<12; i++)
    {
        if(subPages[i]->underMouse())
        {
            clicked_index = i;
        }
    }

    if(clicked_index != -1)
    {
        widgetSetChnl->move(subPages[clicked_index]->pos());
        widgetSetChnl->show();

        subpage_to_be_set = clicked_index;
    }
}

void pagePacketDetail::setChnl(QString str)
{
    qDebug()<<"set to channnel"<<str;

    int index = str.toInt();

    int channel = subPages[subpage_to_be_set]->getIndex();

    channelMapping[index] = channel;

    updateChnlMapping();

    widgetSetChnl->hide();
}

void pagePacketDetail::updateChnlMapping()
{
    for(int i = 0; i < 12; i++)
    {
        subPages[i]->setChannel(-1);

        for(int j = 0; j <6; j++)
        {
            if(subPages[i]->getIndex() == channelMapping[j])
            {
                subPages[i]->setChannel(j);
            }
        }
    }

}

void pagePacketDetail::changePage()
{
    QPushButton *btn_clicked = (QPushButton*)sender();

    if(btn_clicked == btnPre)
    {
        if(pageID != 0) pageID--;

        for(int i = 0; i < 12; i++)
        {
            subPages[i]->setIndex(pageID*12+i);
        }

        updateChnlMapping();

    }
    else
    {
        if(pageID != 7) pageID++;

        for(int i = 0; i < 12; i++)
        {
            subPages[i]->setIndex(pageID*12+i);
        }

        updateChnlMapping();
    }
}
